﻿#if NETFX || NETCORE

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Apewer.Surface
{

    /// <summary>块状进度条。</summary>
    public partial class BlockProgress : BaseControl
    {

        private Color _normalborder = FormsUtility.GraceBorder;
        private Color _normalblock = FormsUtility.GraceBorder;
        private Color _hoverborder = FormsUtility.GraceSilver;
        private Color _hoverblock = FormsUtility.GraceBorder;

        private int _max = 1;
        private int _value = 0;
        private bool _hover = false;

        /// <summary></summary>
        public BlockProgress()
        {
            this.Size = new Size(200, 40);
            DrawProgress();
            this.Resize += Event_This_Resize;
            this.MouseMove += Event_This_MouseMove;
            this.MouseLeave += Event_This_MouseLeave;
        }

        private void Event_This_MouseLeave(object sender, EventArgs e)
        {
            Hover = false;
        }

        private void Event_This_MouseMove(object sender, MouseEventArgs e)
        {
            Hover = true;
        }

        private void Event_This_Resize(object sender, EventArgs e)
        {
            DrawProgress();
        }

        private bool Hover
        {
            get { return _hover; }
            set
            {
                if (_hover != value)
                {
                    _hover = value;
                    DrawProgress();
                }
            }
        }

        /// <summary>当前进度值。</summary>
        public int Value
        {
            get { return _value; }
            set
            {
                _value = (value < 0) ? 0 : ((value > _max) ? _max : value);
                DrawProgress();
            }
        }

        /// <summary>最大进度值。</summary>
        public int Max
        {
            get { return _max; }
            set
            {
                _max = (value > 0) ? value : 1;
                if (_value >= _max) _value = value;
                DrawProgress();
            }
        }

        /// <summary>增加当前进度值，不会超过最大进度值。</summary>
        public void Plus()
        {
            Plus(1);
        }

        /// <summary>增加当前进度值，不会超过最大进度值。</summary>
        /// <param name="step">进度步长值。</param>
        public void Plus(int step)
        {
            Value = Value + 1;
        }

        private void DrawProgress()
        {
            if (this.IsHandleCreated)
            {
                this.BeginInvoke(new Invoker(delegate () { DrawImage(); }));
            }
            else
            {
                DrawImage();
            }
        }

        private void DrawImage()
        {
            if (this.Width < 1) return;
            if (this.Height < 1) return;

            var vbitmap = new Bitmap(this.Width, this.Height);
            using (var vgraphic = Graphics.FromImage(vbitmap))
            {
                vgraphic.SmoothingMode = SmoothingMode.HighSpeed;
                vgraphic.CompositingMode = CompositingMode.SourceCopy;
                if ((this.Width >= 3) && (this.Height >= 3))
                {
                    vgraphic.Clear(Color.White);
                    using (var vpen = new Pen(Hover ? _hoverborder : _normalborder))
                    {
                        vgraphic.DrawRectangle(vpen, 0, 0, this.Width - 1, this.Height - 1);
                    }
                    if ((this.Width >= 5) && (this.Height >= 5))
                    {
                        int vwidth;
                        if (Value == 0) vwidth = 0;
                        else
                        {
                            if (Value == Max) vwidth = this.Width - 4;
                            else vwidth = (int)(((float)Value / (float)Max) * (float)(this.Width - 4));
                        }
                        using (var vbrush = new SolidBrush(Hover ? _hoverblock : _normalblock))
                        {
                            vgraphic.FillRectangle(vbrush, 2, 2, vwidth, this.Height - 4);
                        }
                    }
                }
                else
                {
                    vgraphic.Clear(Hover ? _hoverblock : _normalblock);
                }
            }
            ProgressImage = vbitmap;
        }

        private Image ProgressImage
        {
            set
            {
                if (this.BackgroundImage != null) this.BackgroundImage.Dispose();
                this.BackgroundImage = value;
            }
        }

    }
    
}

#endif
